<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2021/1/14
 * Time: 16:58
 */

namespace app\admin\controller;


use think\Db;
use think\Request;

class Companyinfo extends Base
{
    /**
     * 首页展示
     * @return mixed|\think\response\Json
     */
    public function index(){
        if(request()->isAjax ()){
            $create_start_time = input('create_start_time');
            $create_end_time = input('create_end_time');
            $ci_is_top = input('ci_is_top');
            $ci_title = input('ci_title');
            $where = [];
            // 标题
            if(isset($ci_title) && $ci_title != ''){
                $where['ci_title'] = ['like', '%' . $ci_title . '%'];
            }
            // 状态
            if(isset($ci_is_top) && $ci_is_top != ''){
                $where['ci_is_top'] = $ci_is_top;
            }
            // 创建时间
            if(isset($create_start_time) && $create_start_time != '' && isset($create_end_time) && $create_end_time != ''){
                $where['ci_time'] = ['between', [$create_start_time, $create_end_time]];
            }
            if(isset($create_start_time) && $create_start_time != '' && isset($create_end_time) && $create_end_time == ''){
                $where['ci_time'] = ['>', $create_start_time];
            }
            if(isset($create_start_time) && $create_start_time == '' && isset($create_end_time) && $create_end_time != ''){
                $where['ci_time'] = ['<', $create_end_time];
            }
            
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('company_info')->where($where)->count();// 获取总条数
            $lists = Db::name('company_info')
                ->field('ci_id, ci_title, ci_thumb, ci_is_top, ci_time, ci_sort')
                ->where($where)
                ->page($Nowpage,$limits)
                ->order('ci_sort desc, ci_time desc')
                ->select();
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch();
    }
    
    /**
     * 添加企业资讯
     * @return mixed|\think\response\Json
     */
    public function infoAdd(){
        if(Request::instance()->isPost()){
            $ci_title = trim(input('ci_title'));
            $ci_thumb = input('ci_thumb');
            $ci_content = input('ci_content');
            $ci_is_top = input('ci_is_top');
            $ci_sort = input('ci_sort');

            $insert = [
                'ci_title' => $ci_title,
                'ci_thumb' => $ci_thumb,
                'ci_content' => $ci_content,
                'ci_is_top' => $ci_is_top,
                'ci_sort' => $ci_sort,
                'ci_time' => date('Y-m-d H:i:s'),
                'ci_date' => date('Y-m-d')
            ];
            $res = Db::table('up_company_info')->insert($insert);
            if($res){
                return json(['code' => 200, 'msg' => '添加成功']);
            }else{
                return json(['code' => 100, 'msg' => '添加失败']);
            }
        }
        return $this->fetch('companyinfo/infoadd');
    }
    
    /**
     * 编辑企业资讯
     * @return mixed|\think\response\Json
     */
    public function infoEdit(){
        $ci_id = input('ci_id');
        if(Request::instance()->isPost()){
            $ci_title = trim(input('ci_title'));
            $ci_thumb = input('ci_thumb');
            $ci_content = input('ci_content');
            $ci_is_top = input('ci_is_top');
            $ci_sort = input('ci_sort');

            $update = [
                'ci_title' => $ci_title,
                'ci_thumb' => $ci_thumb,
                'ci_content' => $ci_content,
                'ci_is_top' => $ci_is_top,
                'ci_sort' => $ci_sort,
                'ci_updatetime' => date('Y-m-d H:i:s'),
            ];
            $res = Db::table('up_company_info')->where('ci_id', $ci_id)->update($update);
            if($res){
                return json(['code' => 200, 'msg' => '编辑成功']);
            }else{
                return json(['code' => 100, 'msg' => '编辑失败']);
            }
        }
        $info = Db::table('up_company_info')
            ->field('ci_title, ci_thumb, ci_content, ci_is_top, ci_sort')
            ->where('ci_id', $ci_id)
            ->find();
        return $this->fetch('companyinfo/infoedit', ['info' => $info, 'ci_id' => $ci_id]);
    }
    
    /**
     * 企业资讯详情
     * @return mixed
     */
    public function infoDetail(){
        $ci_id = input('ci_id');
        $info = Db::table('up_company_info')
            ->where('ci_id', $ci_id)
            ->field('ci_title, ci_thumb, ci_content, ci_is_top')
            ->find();
        return $this->fetch('companyinfo/infodetail', ['info' => $info]);
    }
    
    /**
     * 置顶
     * @return \think\response\Json
     */
    public function setTop(){
        $ci_id = input('id');
        $res = Db::table('up_company_info')->where('ci_id', $ci_id)->setField('ci_is_top', 1);
        if($res){
            return json(['code' => 200, 'msg' => '置顶成功']);
        }else{
            return json(['code' => 100, 'msg' => '置顶失败']);
        }
    }
    
    /**
     * 取消置顶
     * @return \think\response\Json
     */
    public function setUntop(){
        $ci_id = input('id');
        $res = Db::table('up_company_info')->where('ci_id', $ci_id)->setField('ci_is_top', 2);
        if($res){
            return json(['code' => 200, 'msg' => '取消置顶成功']);
        }else{
            return json(['code' => 100, 'msg' => '取消置顶失败']);
        }
    }
    
    /**
     * 删除企业资讯
     * @return \think\response\Json
     */
    public function infoDel(){
        $ci_id = input('id');
        $res = Db::table('up_company_info')->delete($ci_id);
        if($res){
            return json(['code' => 200, 'msg' => '删除成功']);
        }else{
            return json(['code' => 100, 'msg' => '删除失败']);
        }
    }
}